package cn.iocoder.yudao.module.erp.controller.admin.dp.dpbisk;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.erp.controller.admin.dispatchvehicle.vo.DispatchVehicleRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.dispatchvehicle.vo.DispatchVehicleSaveReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.dp.dpalmt.vo.DpAlmtPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.dp.dpalmt.vo.DpAlmtRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.dp.dpalmt.vo.DpAlmtSaveReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.dp.dpwsl.vo.DpWslPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.dp.dpwsl.vo.DpWslRespVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.dispatchvehicle.DispatchVehicleDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.dp.DpAlmtDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.dp.DpBiskDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.dp.DpWslDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.dpitem.DpItemBiskDO;
import cn.iocoder.yudao.module.erp.controller.admin.dp.dpbisk.vo.DpBiskSaveReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.dp.dpbisk.vo.DpBiskRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.dp.dpbisk.vo.DpBiskPageReqVO;
import cn.iocoder.yudao.module.erp.service.dp.DpBiskService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.List;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

@Tag(name = "管理后台 - ERP比什凯克仓库")
@RestController
@RequestMapping("/erp/dp-bisk")
@Validated
public class DpBiskController {

    @Resource
    private DpBiskService dpBiskService;

    @PostMapping("/create")
    @Operation(summary = "创建ERP比什凯克仓库")
//    @PreAuthorize("@ss.hasPermission('erp:dp-bisk:create')")
    public CommonResult<String> createDpBisk(@Valid @RequestBody DpBiskSaveReqVO createReqVO) {
        return success(dpBiskService.createDpBisk(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ERP比什凯克仓库")
//    @PreAuthorize("@ss.hasPermission('erp:dp-bisk:update')")
    public CommonResult<Boolean> updateDpBisk(@Valid @RequestBody DpBiskSaveReqVO updateReqVO) {
        dpBiskService.updateDpBisk(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除ERP比什凯克仓库")
    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('erp:dp-bisk:delete')")
    public CommonResult<Boolean> deleteDpBisk(@RequestParam("id") String id) {
        dpBiskService.deleteDpBisk(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ERP比什凯克仓库")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('erp:dp-bisk:query')")
    public CommonResult<DpBiskRespVO> getDpBisk(@RequestParam("id") String id) {
        DpBiskDO dpBisk = dpBiskService.getDpBisk(id);
        return success(BeanUtils.toBean(dpBisk, DpBiskRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ERP比什凯克仓库分页")
//    @PreAuthorize("@ss.hasPermission('erp:dp-bisk:query')")
    public CommonResult<PageResult<DpBiskRespVO>> getDpBiskPage(@Valid DpBiskPageReqVO pageReqVO) {
        PageResult<DpBiskDO> pageResult = dpBiskService.getDpBiskPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DpBiskRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ERP比什凯克仓库 Excel")
//    @PreAuthorize("@ss.hasPermission('erp:dp-bisk:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDpBiskExcel(@Valid DpBiskPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DpBiskDO> list = dpBiskService.getDpBiskPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ERP比什凯克仓库.xls", "数据", DpBiskRespVO.class,
                        BeanUtils.toBean(list, DpBiskRespVO.class));
    }

    @GetMapping("/dpBiskOuterList")
    @Operation(summary = "获得ERP比什凯克制单出库列表")
//    @PreAuthorize("@ss.hasPermission('erp:dp-bisk:outer')")
    public CommonResult<PageResult<DpBiskRespVO>> dpBiskOuterList(@Valid DpBiskPageReqVO pageReqVO) {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        PageResult<DpBiskDO> dpBiskOuterList = dpBiskService.dpBiskOuterList(pageReqVO);
        return success(BeanUtils.toBean(dpBiskOuterList, DpBiskRespVO.class));
    }

    @PostMapping("/dpBiskOutStroe")
    @Operation(summary = "ERP比什凯克仓库单据配车出库")
//    @PreAuthorize("@ss.hasPermission('erp:dp-bisk:outer')")
    public CommonResult<Boolean> dpBiskOutStroe(@Valid @RequestBody DispatchVehicleSaveReqVO createReqVO) {
        dpBiskService.dpBiskOutStroe(createReqVO);
        return success(true);
    }

    // ==================== 子表（ERP比什凯克制单货号详情） ====================

    @GetMapping("/dp-item-bisk/list-by-no")
    @Operation(summary = "获得ERP比什凯克制单货号详情列表")
    @Parameter(name = "no", description = "运单号")
//    @PreAuthorize("@ss.hasPermission('erp:dp-bisk:detail')")
    public CommonResult<List<DpItemBiskDO>> getDpItemBiskListByNo(@RequestParam("no") String no) {
        return success(dpBiskService.getDpItemBiskListByNo(no));
    }

    @PostMapping("/enterStroeDpBisk")
    @Operation(summary = "ERP比什凯克运单入库")
//    @PreAuthorize("@ss.hasPermission('erp:dp-bisk:enter')")
    public CommonResult<Boolean> enterStroeDpBisk(@Valid @RequestBody List<DispatchVehicleSaveReqVO> createReqVO) {
        dpBiskService.enterStroeDpBisk(createReqVO);
        return success(true);
    }

    @GetMapping("/dpBiskEnterList")
    @Operation(summary = "获得ERP比什凯克制单入库列表")
//    @PreAuthorize("@ss.hasPermission('erp:dp-bisk:enter')")
    public CommonResult<List<DispatchVehicleRespVO>> dpBiskEnterList(@RequestParam(value = "dispatchNumber",required = false) String dispatchNumber,
                                                                     @RequestParam(value = "line",required = false) Integer line,
                                                                     @RequestParam(value = "dest",required = false) Integer dest,
                                                                     @RequestParam(value = "dpId",required = false) String dpId) {
        List<DispatchVehicleDO> dpBiskEnterList = dpBiskService.dpBiskEnterList(dispatchNumber,line,1, dest,dpId);
        return success(BeanUtils.toBean(dpBiskEnterList, DispatchVehicleRespVO.class));
    }

    @PostMapping("/signDp")
    @Operation(summary = "ERP到货签收")
//    @PreAuthorize("@ss.hasPermission('erp:dp-bisk:enter')")
    public CommonResult<Boolean> signDp(@Valid @RequestBody DpBiskSaveReqVO updateReqVO) {
        dpBiskService.signDp(updateReqVO);
        return success(true);
    }
    
}